﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.Base;
using QLBH.StandardUI;

namespace Module.SystemUI
{
    public partial class frmRegisterInformation : Form
    {
        private CUAHANG currentCuaHang;
        public frmRegisterInformation()
        {
            InitializeComponent();
        }       

        private void frmRegisterInformation_Load(object sender, EventArgs e)
        {
            currentCuaHang = CuaHangController.GetInstance().GetAll().FirstOrDefault();

            OnSetModeltoControl();
        }

        private void OnSetModeltoControl()
        {
            tbxTenCH.Text = currentCuaHang.TENCH;
            rtbxDiaChi.Text = currentCuaHang.DIACHI;
            tbxDienThoai.Text = currentCuaHang.DIENTHOAI;
            tbxFax.Text = currentCuaHang.FAX;
            tbxWebsite.Text = currentCuaHang.WEBSITE;

            nudBBDefault.Value = (int)currentCuaHang.BBDEFAULT;
            nudBLDefault.Value = (int)currentCuaHang.BLDEFAULT;
            nudHetHangWarning.Value = (int)currentCuaHang.HETHANGWAR;
            nudNoQuaHanWarning.Value = (int)currentCuaHang.NOQUAHANWAR;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckValidInputData())
                {
                    currentCuaHang.TENCH = tbxTenCH.Text;
                    currentCuaHang.DIACHI = rtbxDiaChi.Text;
                    currentCuaHang.DIENTHOAI = tbxDienThoai.Text;
                    currentCuaHang.FAX = tbxFax.Text;
                    currentCuaHang.HETHANGWAR = (int)nudHetHangWarning.Value;
                    currentCuaHang.NOQUAHANWAR = (int)nudNoQuaHanWarning.Value;

                    CuaHangController.GetInstance().Edit(currentCuaHang);

                    ShowMessage.Instance.ShowInfo("Dữ liệu được lưu thành công");

                    this.Close();                        
                }
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }            
        }

        private bool CheckValidInputData()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxTenCH.Text, "Tên cửa hàng", 100);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxTenCH.SelectAll();
                tbxTenCH.Focus();
                return false;
            }

            try
            {
                DataInputHelper.CheckPhoneNumber(tbxDienThoai.Text);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxDienThoai.SelectAll();
                tbxDienThoai.Focus();
                return false;
            }

            try
            {
                DataInputHelper.CheckFaxNumber(tbxFax.Text);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxFax.SelectAll();
                tbxFax.Focus();
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
